﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using WFNetLib.MyControls;
using 数据函数库;
using WFNetLib.TCP;
using WFNetLib;

namespace 温室大棚
{
    public partial class Form远程控制 : Form
    {
        TCPAsyncServer tcpAsyncServer;
        Hashtable CurGreenhouseList;
        public Form远程控制(Hashtable ht, TCPAsyncServer _tcpAsyncServer)
        {
            InitializeComponent();
            CurGreenhouseList = new Hashtable();
            foreach (DictionaryEntry de in ht)
            {
                GreenhouseInfo gf = (GreenhouseInfo)de.Value;
                string str = gf.ip + " 大棚号: " + gf.GreenhouseAddr.ToString() + " 节点号:" + gf.NoteNum.ToString();
                ComboBoxItem c = new ComboBoxItem(str, de.Key);
                toolStripComboBox1.Items.Add(c);
                CurGreenhouseList.Add(de.Key, de.Value);
            }
            tcpAsyncServer = _tcpAsyncServer;
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (toolStripComboBox1.SelectedItem == null)
            {
                MessageBox.Show("请选择控制目标!");
                return;
            }
            ComboBoxItem c = (ComboBoxItem)toolStripComboBox1.SelectedItem;
            GreenhouseInfo gf = (GreenhouseInfo)CurGreenhouseList[c.Value];
            byte[] tx = new byte[4];
            if (checkBox1.Checked)
                tx[0] = 1;
            else
                tx[0] = 0;
            if (checkBox2.Checked)
                tx[1] = 1;
            else
                tx[1] = 0;
            if (checkBox3.Checked)
                tx[2] = 1;
            else
                tx[2] = 0;
            if (checkBox4.Checked)
                tx[3] = 1;
            else
                tx[3] = 0;
            tcpAsyncServer.Send(c.Value.ToString(), TcpPacket.MakeTcpPacket(4, (UInt16)gf.GreenhouseAddr, (byte)gf.NoteNum, tx));
            timer1.Enabled = true;
            bWait = true;
            toolStripButton1.Enabled = false;
        }
        public void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            GreenhouseInfo gf;
            this.Invoke((EventHandler)(delegate
            {
                try
                {
                    TcpPacket[] rxList = (TcpPacket[])e.netPacket;
                    foreach (TcpPacket rx in rxList)
                    {
                        switch (rx.Header.Command)
                        {
                            case 1:
                                实时数据Data ssData = DataAnalysis.实时数据DataAnalysis(rx);
                                gf = (GreenhouseInfo)CurGreenhouseList[e.Client.key];
                                if (gf == null)
                                {
                                    gf = new GreenhouseInfo();
                                    gf.ip = e.Client.ClientSocket.RemoteEndPoint.ToString();
                                    gf.GreenhouseAddr = ssData.GreenhouseAddr;
                                    gf.NoteNum = ssData.NoteNum;
                                    CurGreenhouseList.Add(e.Client.key, gf);
                                    string str = e.Client.ClientSocket.RemoteEndPoint.ToString() + " 大棚号: " + ssData.GreenhouseAddr.ToString() + " 节点号:" + ssData.NoteNum.ToString();
                                    ComboBoxItem c = new ComboBoxItem(str, e.Client.key);
                                    toolStripComboBox1.Items.Add(c);
                                }                                                
                                break;
                            case 2:
                                大棚信息Data dpData = DataAnalysis.大棚信息DataAnalysis(rx);
                                UInt16 oldGreenhouseAddr = (UInt16)dpData.GreenhouseAddr;
                                byte oldNoteNum = (byte)dpData.NoteNum;
                                UInt16 newGreenhouseAddr = BytesOP.MakeShort(rx.Data[0], rx.Data[1]);
                                byte newNoteNum = (byte)rx.Data[2];
                                gf = (GreenhouseInfo)CurGreenhouseList[e.Client.key];
                                if (gf != null)
                                {
                                    gf.GreenhouseAddr = newGreenhouseAddr;
                                    gf.NoteNum = newNoteNum;
                                    CurGreenhouseList[e.Client.key] = gf;
                                    for (int i = 0; i < toolStripComboBox1.Items.Count; i++)
                                    {
                                        ComboBoxItem c=(ComboBoxItem)toolStripComboBox1.Items[i];
                                        if (c.Value == e.Client.key)
                                        {
                                            string str = e.Client.ClientSocket.RemoteEndPoint.ToString() + " 大棚号: " + newGreenhouseAddr.ToString() + " 节点号:" + newNoteNum.ToString();
                                            toolStripComboBox1.Items[i] = new ComboBoxItem(str, e.Client.key);
                                        }
                                    }                                    
                                }
                                else
                                {
                                    gf = new GreenhouseInfo();
                                    gf.ip = e.Client.ClientSocket.RemoteEndPoint.ToString();
                                    gf.GreenhouseAddr = newGreenhouseAddr;
                                    gf.NoteNum = newNoteNum;
                                    CurGreenhouseList.Add(e.Client.key, gf);
                                    string str = e.Client.ClientSocket.RemoteEndPoint.ToString() + " 大棚号: " + newGreenhouseAddr.ToString() + " 节点号:" + newNoteNum.ToString();
                                    ComboBoxItem c = new ComboBoxItem(str, e.Client.key);
                                    toolStripComboBox1.Items.Add(c);
                                }                                    
                                break;
                            case 4:
                                if (bWait)
                                {
                                    timer1.Enabled = false;
                                    toolStripButton1.Enabled = true;
                                    MessageBox.Show("设定成功");
                                    bWait = false;
                                }
                                break;
                        }                        
                    }
                }
                catch (System.Exception ex)
                {
                    
                }
            }));
        }
        bool bWait;
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                checkBox1.Text = "遮阳网 开";
            }
            else
            {
                checkBox1.Text = "遮阳网 关";
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox2.Text = "灌溉阀 开";
            }
            else
            {
                checkBox2.Text = "灌溉阀 关";
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                checkBox3.Text = "照明 开";
            }
            else
            {
                checkBox3.Text = "照明 关";
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                checkBox4.Text = "排风 开";
            }
            else
            {
                checkBox4.Text = "排风 关";
            }
        }

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            checkBox1.Text = "遮阳网 关";
            checkBox2.Text = "灌溉阀 关";
            checkBox3.Text = "照明 关";
            checkBox4.Text = "排风 关";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            if (bWait)
            {
                bWait = false;
                MessageBox.Show("设定失败");
                toolStripButton1.Enabled = true;
            }
        }
        
    }
}
